<script lang="ts" setup>
import { postCouponIdApi } from '@/services/home'
import { Toast } from '@/utils/tools'
import { ref, reactive } from 'vue'

const { couponItem } = defineProps<{
  couponItem?: any
}>()

/**点击领取按钮 */
const receive = async (couponId: number) => {
  const res = await postCouponIdApi(couponId)
  if (res && res.code === 200) {
    Toast('领取成功！优惠券已放入您的卡包啦')
  } else {
    Toast(res.msg)
  }
  emits('refresh')
}

const goEat = () => {
  uni.switchTab({
    url: '/pages/eat/eat',
  })
}

/** 传入值返回优惠券规则 */
const convertText = (item: any) => {
  if (item.threshold) {
    return `满${item.threshold}元可用`
  } else if (!item.threshold && item.discountsType === 2) {
    return `无门槛`
  } else {
    return `无门槛立减`
  }
}

const emits = defineEmits<{
  (event: 'triggerRules'): void
  (event: 'refresh'): void
}>()
</script>

<template>
  <view class="couponItem_box">
    <view class="_left">
      <!-- <view class="couponType">{{
        couponItem?.type === 1
          ? '满减券'
          : couponItem?.type === 2
          ? '折扣券'
          : couponItem?.type === 3
          ? '单品券'
          : couponItem?.type === 4
          ? '新人券'
          : couponItem?.type === 5
          ? '霸王免单券'
          : ''
      }}</view> -->
      <view v-if="couponItem?.type === 5" class="freeOfCharge">免单</view>
      <view v-else class="couponPriceBox">
        <view class="couponPrice_box">
          <view class="couponPrice">{{ couponItem?.discounts }}</view>
          {{ couponItem.discountsType === 1 ? '元' : '折' }}
        </view>
        <view class="couponRule">{{ convertText(couponItem) }}</view>
      </view>
      <view class="top-semicircular"></view>
      <view class="bottom-semicircular"></view>
    </view>
    <view class="_right">
      <view class="couponName_box">
        <!-- couponItem.couponName -->
        <view class="couponName">{{ couponItem.couponName }}</view>
        <view @tap="emits('triggerRules')" class="ruleButton">
          <view class="ruleText"> 使用规则 <view class="iconfont icon_questionMark"></view> </view>

          <button
            v-if="!couponItem.clientId"
            hover-class="view-hover"
            @tap="receive(couponItem.id)"
            class="receiveButton"
          >
            立即领取
          </button>
          <button
            v-if="couponItem.clientId"
            hover-class="view-hover"
            @tap="goEat"
            class="useButton"
          >
            立即使用
          </button>
        </view>
      </view>
    </view>
    <!-- <view v-if="couponItem.clientId" class="status">已领取</view> -->
  </view>
</template>

<style lang="scss">
.couponItem_box {
  width: 702rpx;
  height: 212rpx;
  border-radius: 16rpx;
  // background: #fff;
  padding: 4rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  overflow: hidden;
  position: relative;
  ._left {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 212rpx;
    height: 206rpx;
    border-radius: 16rpx 0 0 16rpx;
    background: url('/static/images/couponBg.png') no-repeat;
    background-size: cover;
    color: #fff;
    .couponType {
      position: absolute;
      left: 0;
      top: 0;
      min-width: 92rpx;
      height: 36rpx;
      line-height: 36rpx;
      text-align: center;
      font-size: 20rpx;
      color: #488b16;
      padding: 0 16rpx;
      border-radius: 16rpx 0px;
      background: linear-gradient(90deg, rgba(138, 223, 75, 0.6) 0%, rgba(138, 223, 75, 0) 100%);
    }

    .freeOfCharge {
      font-size: 48rpx;
      color: #fff;
    }
    .couponPriceBox {
      display: flex;
      flex-direction: column;
      align-items: center;
      .couponPrice_box {
        display: flex;
        align-items: baseline;
        min-width: 56rpx;
        height: 68rpx;
        color: #fff;
        font-size: 24rpx;
        .couponPrice {
          font-size: 56rpx;
        }
      }
      .couponRule {
        margin-top: 8rpx;
        min-width: 56rpx;
        height: 34rpx;
        font-size: 24rpx;
        color: #fff;
      }
    }
  }
  ._right {
    flex: 1;
    display: flex;
    align-items: center;
    height: 204rpx;
    position: relative;
    background: #fff;
    border-radius: 0 16rpx 16rpx 0;
    .couponName_box {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      white-space: normal;
      word-wrap: break-word;
      word-break: break-all;
      box-sizing: border-box;
      padding: 28rpx;
      .couponName {
        font-size: 32rpx;
        color: #000;
      }
      .ruleButton {
        margin-top: 8rpx;
        display: flex;
        align-items: center;
        font-size: 24rpx;
        color: #ffbd27;
        justify-content: space-between;
        .icon_questionMark {
          margin-left: 12rpx;
          font-size: 24rpx;
        }
        .ruleText {
          display: flex;
          align-items: center;
        }
      }
    }

    .receiveButton {
      min-width: 132rpx;
      height: 54rpx;
      line-height: 54rpx;
      font-size: 24rpx;
      color: #fff;
      border-radius: 20px;
      background: #ffc83a;
      margin: 0;
    }

    .useButton {
      margin: 0;
      min-width: 132rpx;
      height: 54rpx;
      line-height: 52rpx;
      font-size: 24rpx;
      color: #fff;
      border-radius: 20px;
      background: #ffc83a;
    }
  }
  .status {
    width: 200rpx;
    height: 30rpx;
    position: absolute;
    top: 20rpx;
    right: -68rpx;
    transform: rotate(45deg);
    background: #ff714c;
    color: #fff;
    text-align: center;
    font-family: 'PingFang SC';
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
    line-height: 30rpx;
  }
}
</style>
